package com.meibei.ljl.test01;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by xy on 16/9/26.
 */
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.MyViewHolder> {
    private Context context;
    private List<Msg> list;
    private LayoutInflater inflater;


    public MsgAdapter(Context context, List<Msg> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override//创建ViewHolder
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_msg_type, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override//设置值
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Msg msg = list.get(position);//得到当前的列表项
        if (msg.getType() == Msg.TYPE_RECEIVED) {//判断消息类型
            holder.leftlayout.setVisibility(View.VISIBLE);
            holder.rightlayout.setVisibility(View.GONE);
            holder.chatleft.setText(msg.getContent());
        } else if (msg.getType() == Msg.TYPE_SEND) {
            holder.rightlayout.setVisibility(View.VISIBLE);
            holder.leftlayout.setVisibility(View.GONE);
            holder.chatright.setText(msg.getContent());
        }
    }

    @Override//获取总的条目数
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        LinearLayout leftlayout, rightlayout;
        TextView chatleft, chatright;

        public MyViewHolder(View view) {
            super(view);
            leftlayout = (LinearLayout) view.findViewById(R.id.item_left_layout);
            rightlayout = (LinearLayout) view.findViewById(R.id.item_right_layout);
            chatleft = (TextView) view.findViewById(R.id.item_left_msg);
            chatright = (TextView) view.findViewById(R.id.item_right_msg);
        }
    }
}
